package com.ten.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.DoctorService;
import com.ten.mall.service.UserService;
import com.ten.mall.vo.Doctor;
import com.ten.mall.vo.User;

@Controller
public class DoctorController {
	@Autowired
	private UserService userService;
	@Autowired
	private DoctorService doctorService;
	/**
	 * 保存医生的销售员
	 * @param u
	 * @param res
	 * @return
	 */
	@RequestMapping("/admin/doctorSave")
	public String saveSaleman(Doctor u,HttpServletRequest res){
		if(StringUtils.isNotBlank(u.getId())){
			doctorService.updatePihao(u);
		}
		
		return "redirect:/admin/doctorList";
	}
	
	/**
	 * 给医生指定销售员
	 * @param id
	 * @param res
	 * @return
	 */
	@RequestMapping("/admin/doctoryAdd")
	public String addSaleman(String id,HttpServletRequest res){
		Doctor doctor =doctorService.selectById(id);
		List<User> doctorlist =userService.selectSalemanPages(null,"销售员");
		res.setAttribute("doctor", doctor);
		res.setAttribute("doctorlist", doctorlist);
		return "jsp/doctorAdd";
	}
	/**
	 * 医生管理
	 * @param start
	 * @param size
	 * @param name
	 * @return
	 */
	@RequestMapping("/admin/doctorList")
	public String salemanList(@RequestParam(name="start",defaultValue="1") Integer start,
			   				  @RequestParam(name="size",defaultValue="10") Integer size,
			   				  String name,HttpServletRequest res){
		
		Map<String,Object> map =doctorService.selectPihao(start, size, name);
		List<Doctor> list =(List<Doctor>) map.get("list");
		res.setAttribute("list", list);
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		res.setAttribute("page", start);
		res.setAttribute("total", total);
		return "jsp/doctorList";
	}
	/**
	 * 获取二维码
	 * @param request
	 * @return
	 */
	@RequestMapping("/phone/doctorCode")
	 public String doctorCode(HttpServletRequest request){
		 User u =(User) request.getSession().getAttribute("u");
		 Doctor d =doctorService.selectById(u.getId());
		 request.setAttribute("d", d);
		 return "phone/doctor_code";
	 }
	/**
	 * 查看医生
	 * @param start
	 * @param size
	 * @param request
	 * @return
	 */
	@RequestMapping("/phone/my_doctor")
	public String myDoctor(HttpServletRequest request){
		User u =(User) request.getSession().getAttribute("u");
		List<User> list =userService.selectDoctor(1, 100, u.getRealName());
		request.setAttribute("list", list);
		return "phone/my_doctor";
	}
	/**
	 * 删除医生信息
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/admin/doctorDel")
	@ResponseBody
	public String delDoctor(HttpServletRequest request,String id){
		String rs="error";
		
		try {
			if(StringUtils.isNotBlank(id)){
				doctorService.deletePihao(id);
				rs ="ok";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	/**
	 * 按时间查询订单
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping("/phone/doctorSearch")
	public String toDoctorSearch(HttpServletRequest request,String userId){
		request.setAttribute("userId", userId);
		return "phone/doctor_search";
	}
}
